<!--
 * @Descripttion: 
 * @version: 修改房号
 * @Author: mmzhang
 * @Date: 2021-06-29 11:07:33
 * @LastEditTime: 2021-09-24 11:29:48
-->
<template>
  <div class="edit-room-code">
    <Table ref="TableHeight" :columns="roomColumn" :max-height="400" :data="roomList">
    </Table>
    <div class="flex flex-horizontal-end btn-operate">
      <Button @click="handleClose">取消</Button>
      <Button type="primary" @click="submitFun()" style="margin-left: 8px">确定</Button>
    </div>
  </div>
</template>
<script>
import roomMgr from '@/biz/modeling/room/room'
export default {
  props: ['roomList'],
  data() {
    let _this = this
    return {
      roomColumn: [
        {
          title: '序号',
          type: 'index',
          minWidth: 120,
        },
        {
          title: '原房间号',
          key: 'roomCode',
          minWidth: 120,
        },
        {
          title: '新房间号',
          key: 'newCode',
          minWidth: 120,
          render(h, params) {
            return h('Input', {
              props: {
                type: 'text',
                value: params.row.newCode,
                placeholder: '请输入房号',
              },
              on: {
                'on-blur': (event) => {
                  params.row.newCode = event.target.value
                  _this.roomList[params.index].newCode = event.target.value
                },
              },
            })
          },
        },
      ],
    }
  },
  created() {
    this.roomList.forEach((item) => {
      item.newCode = ''
    })
  },
  mounted() {},
  methods: {
    handleClose() {
      this.$emit('onClose')
    },
    submitFun() {
      let roomArr = []
      // 是否有新的房号
      if (this.roomList.some((item) => item.newCode == '')) {
        this.$Message.error('请填写新的房号！')
        return false
      }
      // 判断是否存在相同单元相同房号的情况
      let filterArr = []
      for (const item of this.roomList) {
        // 房间号不允许出现特殊字符
        if (this.$tool.unNormalReg(item.newCode)) {
          this.$Message.error('房号不能填写特殊字符或空格！')
          return false
        }
        filterArr.push(
          item.buildingId.toString() +
            ',' +
            item.unitId.toString() +
            ',' +
            item.newCode.toString()
        )
      }
      let filterKeyArr = Array.from(new Set(filterArr))

      if (filterKeyArr.length != filterArr.length) {
        this.$Message.error(
          '请检查，存在相同楼栋，相同单元，相同房间号的情况！'
        )
        return false
      }
      // 深拷贝
      let roomCheck = JSON.parse(JSON.stringify(this.roomList))

      // 提交
      roomCheck.forEach((ele) => {
        ele.roomCode = ele.newCode ? ele.newCode : ele.roomCode
        roomArr.push(ele)
      })
      let params = {
        roomEditVOS: roomArr,
      }
      roomMgr.editRoomFun(params).then((res) => {
        this.$Message.info('修改成功。')
        this.$emit('onClose')
        this.$emit('onSearch')
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.edit-room-code {
  .btn-operate {
    margin-top: 20px;
  }
}
</style>